package cn.schoolwow.quickdao.module.database.ddl.action.service.table;

import cn.schoolwow.quickdao.domain.QuickDAOConfig;
import cn.schoolwow.quickdao.domain.database.parent.DatabaseType;
import cn.schoolwow.quickdao.domain.entity.Entity;
import cn.schoolwow.quickdao.module.database.ddl.action.flow.table.*;
import cn.schoolwow.quickdao.module.database.ddl.action.flow.table.common.ExecuteCreateTableFlow;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;

public class CreateTableFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        Entity entity = flowContext.checkData("entity", Entity.class);
        QuickDAOConfig quickDAOConfig = flowContext.checkInstanceData(QuickDAOConfig.class);

        if(entity.properties.isEmpty()){
            StringBuilder sqlBuilder = new StringBuilder("create table " + quickDAOConfig.databaseContext.databaseProvider.escape(entity.tableName) + "();");
            flowContext.putTemporaryData("sqlBuilder", sqlBuilder);
        }else{
            DatabaseType databaseType = flowContext.checkInstanceData(DatabaseType.class);
            switch (databaseType){
                case SQLite:{
                    flowContext.executeFlowList(new SQLiteCreateTableFlow());
                }break;
                case H2:
                case Mysql:
                case MariaDB:{
                    flowContext.executeFlowList(new MysqlCreateTableFlow());
                }break;
                case Postgresql:{
                    flowContext.executeFlowList(new PostgresCreateTableFlow());
                }break;
                case SQLServer:{
                    flowContext.executeFlowList(new SQLServerCreateTableFlow());
                }break;
                case Oracle:{
                    flowContext.executeFlowList(new OracleCreateTableFlow());
                }break;
            }
        }
        flowContext.executeFlowList(new ExecuteCreateTableFlow());
    }

    @Override
    public String name() {
        return "创建数据库表";
    }

}
